package doublepoint;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/4 0:00
 * https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
 */
public class RemoveNthFromEnd {
    class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        //firstNode和secondNode之间相差n+1个节点
        ListNode firstNode = dummyNode;
        ListNode secondNode = dummyNode;
        for (int i = 0; i <= n; i++) {
            firstNode = firstNode.next;
        }
        while (firstNode != null) {
            firstNode = firstNode.next;
            secondNode = secondNode.next;
        }
        secondNode.next = secondNode.next.next;
        return dummyNode.next;
    }

    public static void main(String[] args) {

    }
}
